//
//  ZKJScrollCollection.m
//  ScrollCollectionVIew
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 ZKJ. All rights reserved.
//

#import "ZKJScrollCollection.h"
#import "ZKJScrollCollectionViewCell.h"
#import "UIImageView+WebCache.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@implementation ZKJScrollCollection

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _count = 2;
        
        self.flag = YES;
        
        [self addSubview:self.collectionView];
        
        [self addSubview:self.pageControl];
        
    }
    return self;
}

#pragma mark - pageControlFrame
// 设置pageControl的frame
- (void)setMyPageControlFrame:(CGRect )frame {
    self.pageControl.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
}

#pragma mark - loadPicture

- (void)loadPicture:(NSArray *)imageArray withUrlArray:(NSArray *)urlArray {
    self.imageArray = [[NSMutableArray alloc] initWithArray:imageArray];
    self.urlArray = [[NSMutableArray alloc] initWithArray:urlArray];
    
    if (self.imageArray.count) {
        
        [self.imageArray insertObject:[self.imageArray lastObject] atIndex:0];
        [self.imageArray addObject:self.imageArray[1]];
        
        [self.urlArray insertObject:[self.urlArray lastObject] atIndex:0];
        [self.urlArray addObject:self.urlArray[1]];
        
        _count = self.imageArray.count;
        _pageControl.numberOfPages = _count - 2;
        
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });

}

#pragma mark - 自动播放

// 自动播放
- (void)setTimer:(NSTimeInterval)timeinterval {
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(timerCarousel) userInfo:nil repeats:YES];
    
    [self.myTimer fire];
}
// 自动播放方法
- (void)timerCarousel {
    self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x + WIDTH, 0);
    if (self.collectionView.contentOffset.x > WIDTH * (_count - 2)) {
        self.collectionView.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.pageControl.currentPage = self.collectionView.contentOffset.x / WIDTH - 1;
    
    
    //    添加自定义翻页动画
    CATransition *animation = [CATransition animation];
    //    设置动画执行时间
    animation.duration = 1;
    //    设置动画类型 cube立方
    [animation setType:@"moveIn"];
    //    动画开始方向
    animation.subtype = kCATransitionFromRight;
    [self.collectionView.layer addAnimation:animation forKey:nil];
}

#pragma mark - 轮播方法

// 实现轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        if (scrollView.contentOffset.x == WIDTH * (_count - 1)) {
            scrollView.contentOffset = CGPointMake(WIDTH, 0);
        } else if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(WIDTH * (_count - 2), 0);
        }
        //    根据图片改currentPage
        self.pageControl.currentPage = self.collectionView.contentOffset.x / WIDTH - 1;
        
//        //    添加自定义翻页动画
//        CATransition *animation = [CATransition animation];
//        //    设置动画执行时间
//        animation.duration = 1;
//        //    设置动画类型 cube立方
//        [animation setType:@"moveIn"];
//        //    动画开始方向
//        animation.subtype = kCATransitionFromRight;
//        [self.collectionView.layer addAnimation:animation forKey:nil];
    }
    
}

//pageControl方法
- (void)changePageByPageControl:(UIPageControl *)sender {
    self.collectionView.contentOffset = CGPointMake(WIDTH * (sender.currentPage + 1), 0);
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZKJScrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZKJScrollCollectionViewCellIdentifier" forIndexPath:indexPath];
    
    if (self.imageArray.count > indexPath.item) {
        [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.item]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"#######");
    NSLog(@"%@", self.urlArray[indexPath.item]);
    [self.delegate gotoWebView:self.urlArray[indexPath.item]];
}

#pragma mark - private

//collectionView懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.contentOffset = CGPointMake(_collectionView.frame.size.width, 0);
        
        [_collectionView registerClass:[ZKJScrollCollectionViewCell class] forCellWithReuseIdentifier:@"ZKJScrollCollectionViewCellIdentifier"];
        
    }
    return _collectionView;
}

// imageArray懒加载
- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        self.imageArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _imageArray;
}

- (NSMutableArray *)urlArray {
    if (!_urlArray) {
        self.urlArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _urlArray;
}

// pageControl懒加载
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(350, HEIGHT - 30, 50, 20)];
        _pageControl.center = CGPointMake(WIDTH / 2, HEIGHT - 15);
        _pageControl.numberOfPages = _count - 2;
        
        _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        
        [_pageControl addTarget:self action:@selector(changePageByPageControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
